package com.bqtt.tumor.demo.common.utility;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import org.tbwork.utility.toolbox.tbString;
 

public class InlineTools {


	public static String checkedStyle="checked=true";
	public static String nocheckedStyle="";
	
	public static String imgLocalPathDirectory = "";
	public static String imgWebPathDirectory = "";
    public static String object2SQLString(Object value)
    {
    	if(value==null)
    		return "";
    	String stringValue = "";
    	if (value.getClass() == Integer.class
				|| value.getClass() == Double.class
				|| value.getClass() == Long.class)
			stringValue = value.toString();
		else
			stringValue = "'" + value.toString() + "'";
    	
    	return stringValue;
    }
	
    public static String getExtName(String name)
    {
    	for(int i = name.length()-1;i>=0;i--)
    	{
    		if(name.charAt(i)=='.')
    		{
    			return name.substring(i+1);
    		}
    	}
    	return name;
    }
    
    public static String object2SQLFuzzyMatchString(Object value)
    {
    	if(value==null)
    		return "";
    	String stringValue = "";
    	if (value.getClass() == Integer.class
				|| value.getClass() == Double.class
				|| value.getClass() == Long.class)
			stringValue = "=" + value.toString();
		else
			stringValue = "like '%" + value.toString() + "%'";
    	
    	return stringValue;
    }
    
    
    public static boolean isEmpty(String a)
    {
    	
    	if(a==null)
    		return true;
    	if(a.isEmpty())
    		return true;
    	return false;
    }
    
    public static String getDateString(String container)
    {
       return 	tbString.jRegex("\\d{1,4}-\\d{1,2}-\\d{1,2}", container);
    }
	
    
    
    
    public static boolean containsItem(String [] array , String item)
    {
    	for(String item_i : array)
    	{
    		if(item_i.equals(item))
    		{
    			return true;
    		}
    	}
    	return false;
    }
    
    
    public static List<String> getProvinceList()
    {
    	List<String> result = new ArrayList<String>();
    	
    	result.add("上海");
    	result.add("北京");
    	result.add("江苏");
    	result.add("浙江");
    	result.add("四川");
    	result.add("贵州");
    	result.add("黑龙江");
    	
    	return result;
    	
    }
    public static String validdateImage(MultipartFile image)
	{
		if(!(image.getOriginalFilename().contains(".dcm")||image.getOriginalFilename().contains(".jpg")||image.getOriginalFilename().contains(".png")))
		{
			return "Only jpg, png or dcm images accepted";
		}
		else
			return "success";
	}
	
	public static String saveImage(String fileName ,MultipartFile image)
	{
		try
		{
			String extName = InlineTools.getExtName(image.getOriginalFilename());
			File file = new File (InlineTools.imgLocalPathDirectory+fileName+"."+extName);
			
			FileUtils.writeByteArrayToFile(file,image.getBytes());
			
			return "resources/medical_images/" +fileName+"."+extName;
		}
		catch(Exception e)
		{
			return "-1";
		}
	}
	
	
	public static void main(String[]args)
    {

		String test= "/D:/Apache/apache-tomcat-7.0.53/webapps/tumorweb/1.txt";
		test  = test.replace("WEB-INF/classes", "resources/medical_images/");
		File file = new File ("/D:/Apache/apache-tomcat-7.0.53/webapps/tumorweb/1.txt");
		
		
		byte [] array = {'1','2','3'};
		
		try {
			FileUtils.writeByteArrayToFile(file,array);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    }
    
    
}
